//
//  BluetoothShooter.m
//  ClondHearingPlinkerBleSDKDemo
//
//  Created by Hao on 2017/8/19.
//  Copyright © 2017年 Edison.3. All rights reserved.
//

#import "BluetoothShooter.h"
#import "EYBluetoothDeviceManager.h"

@interface BluetoothShooter ()<EYBluetoothDeviceManagerDelegate>

@property(nonatomic,strong)EYBluetoothDeviceManager * bleHanle;
@property(nonatomic,strong)CBPeripheral * peripheral;
@end

@implementation BluetoothShooter

- (void)setup {
    self.bleHanle = [EYBluetoothDeviceManager sharedInstance];
    self.bleHanle.isAutoConnect=YES;
    [self.bleHanle initBluetoothDeviceManagerWithDelegate:self];
    [self performSelector:@selector(startScanFun) withObject:nil afterDelay:5];
}

#pragma mark - other fun

-(void)startScanFun{
    [self.bleHanle startScan];
}


-(void)stopScanFun{
    
    [self.bleHanle stopScan];
}

-(void)disConnectFun{
    
    [self.bleHanle disconnect:self.bleHanle.connectedpPeripheral];
}

#pragma mark - EYBluetoothDeviceManagerDelegate
/**
 代表手机蓝牙开关状态
 
 @param isBluetoothPoweredOn 蓝牙是否已关闭
 */
- (void)EYBluetoothDeviceDidUpdateState:(BOOL)isBluetoothPoweredOn{
    
    NSLog(@"蓝牙开关状态:%@",isBluetoothPoweredOn?@"蓝牙开启":@"蓝牙关闭");
}

/**
 扫描到的外设
 
 */
-(void)EYBluetoothDevicePeripheral:(CBPeripheral *)peripheral didDiscoverWithBroadcastData:(NSDictionary *)data{
    self.peripheral = peripheral;
    [self.bleHanle connect:peripheral];
}



/**
 连接外设成功
 
 @param peripheral 连接上的外设
 */
-(void)EYBluetoothDeviceDidConnected:(CBPeripheral*)peripheral{
    
    NSLog(@"蓝牙连接成功回调");
    [self.bleHanle stopScan];
}


/**
 连接外设失败
 
 @param peripheral 连接失败的外设
 */
-(void)EYBluetoothDeviceConnectedFail:(CBPeripheral *)peripheral{
    
    NSLog(@"蓝牙连接失败回调");
}



/**
 外设断开连接
 
 @param peripheral 断开的外设
 */
-(void)EYBluetoothDevicePeripheralDidDisconnect:(CBPeripheral*) peripheral{
    
    NSLog(@"外设断开连接回调");
}


/**
 Global等准备就绪，拿到了写 读  订阅的特征值，比如电压功能，等等都在这里回调之后初始化
 */
-(void)EYBluetoothDeviceManagerIsReady{
    
    NSLog(@"DeviceManagerIsReady回调");
}

/**
 枪的命令
 
 @param gunCommand 命令的类型
 */

-(void)EYBlueReceiveGunCommand:(EYBleGunCommandType)gunCommand{
    
    switch (gunCommand) {
        case EYBleGunCommandTypeLeftFront: {
            NSLog(@"蓝牙枪－左前按下");
            [[NSNotificationCenter defaultCenter] postNotificationName:@"kBlueGunNotification" object:@"EYBleGunCommandTypeLeftFront"];
            
            break;
        }
        case EYBleGunCommandTypeLeftBack: {
            NSLog(@"蓝牙枪－左后按下");
            [[NSNotificationCenter defaultCenter] postNotificationName:@"kBlueGunNotification" object:@"EYBleGunCommandTypeLeftBack"];
            break;
        }
        case EYBleGunCommandTypeRightFront: {
            NSLog(@"蓝牙枪－右前按下");
            [[NSNotificationCenter defaultCenter] postNotificationName:@"kBlueGunNotification" object:@"EYBleGunCommandTypeRightFront"];
            break;
        }
        case EYBleGunCommandTypeRightBack: {
            NSLog(@"蓝牙枪－右后按下");
            [[NSNotificationCenter defaultCenter] postNotificationName:@"kBlueGunNotification" object:@"EYBleGunCommandTypeRightBack"];
            break;
        }
        case EYBleGunCommandTypeTrigger: {
            NSLog(@"蓝牙枪－扳机按下");
            [[NSNotificationCenter defaultCenter] postNotificationName:@"kBlueGunNotification" object:@"EYBleGunCommandTypeTrigger"];
            break;
        }
        default:
            break;
    }
}

@end
